<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<c:set value="<%=request.getContextPath()%>" var="ctx"></c:set>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8">
	<meta name="keywords" content="关键字台">
    <meta name="description" content="描述"> 
    <link href="${ctx}/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${ctx}/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="${ctx}/css/animate.css" rel="stylesheet">
    <link href="${ctx}/css/style.css?v=4.1.0" rel="stylesheet">
    <link href="${ctx}/css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet"> 
	<link href="${ctx}/css/plugins/toastr/toastr.min.css" rel="stylesheet" >
	<title>帮摇积分配置</title>
<!-- Web Uploader -->

</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
           <div class="col-sm-12">
           		<div class="ibox float-e-margins">
           			<div class="ibox-title">
                        <div class="row">
                           <div class="col-sm-12">
                                <h3>帮摇积分配置</h3>
                           </div>
                        </div>
                      </div>
                   

          
	         <div class="ibox-content">
				<form class="form-horizontal" action="${ctx}/yyy/saveScoreConfig"
					name="hbInitform" id="hbInitform" method="post">
					<c:if test="${not empty scoreList }">	
						<c:forEach var="p" items="${scoreList }" varStatus="status">
							<c:if test="${status.index == 0 }">
								<div class="form-group" >
									<label class="col-sm-3 control-label">中奖积分：</label>
									<div class="col-sm-2">
									<input type="text" class="form-control" name="score" placeholder="请输入中奖积分" maxlength="8"
											id="score" value='${p.score_ }' />
									</div>
									<label class="col-sm-2 control-label">中奖概率(%)：</label>
									<div class="col-sm-2">							
											<input type="text" class="form-control" name="rate" placeholder="请输入中奖概率" maxlength="8"
												id="rate" value="${p.rate_}"/>
									</div>
									<button type="button" class="btn btn-primary" onclick="addCol();return false;">添加积分配置</button>
								</div>
							</c:if>
							<c:if test="${status.index != 0 }">	
								<div class="form-group" >
									<label class="col-sm-3 control-label">中奖积分：</label>
									<div class="col-sm-2">
									<input type="text" class="form-control" name="score" placeholder="请输入中奖积分" maxlength="8"
											id="score" value='${p.score_ }' />
									</div>
									<label class="col-sm-2 control-label">中奖概率(%)：</label>
									<div class="col-sm-2">							
											<input type="text" class="form-control" name="rate" placeholder="请输入中奖概率" maxlength="8"
												id="rate" value="${p.rate_}"/>
									</div>
									<button type="button" class="btn btn-danger btn-xs" onclick="delCol(this);return false;">删除</button>
								</div>
							</c:if>
						</c:forEach>
	                </c:if>             
                       
                    <c:if test="${empty scoreList }">
                    
						<div class="form-group" >
							<label class="col-sm-3 control-label">中奖积分：</label>
							<div class="col-sm-2">
							<input type="text" class="form-control" name="score" placeholder="请输入中奖积分" maxlength="8"
									id="num"  />
							</div>
							<label class="col-sm-2 control-label">中奖概率(%)：</label>
							<div class="col-sm-2">							
									<input type="text" class="form-control" name="rate" placeholder="请输入中奖概率" maxlength="8"
										id="rate" />
							</div>
							<button type="button" class="btn btn-primary" onclick="addCol()">添加积分配置</button>
						</div>					
							
                    </c:if> 

						<div class="form-group">
							<label class="col-sm-5 control-label"></label>
							<div class="col-sm-6">
								<button class="btn btn-large btn-primary"  id="saveBtn" name="saveBtn"  onclick="save();return false;">&#8194;保存&#8194;</button>
							</div>
						</div>
					
				</form>
			</div>
		
		 </div>
		 
		 <div class="ibox float-e-margins">
   			  <div class="ibox-title">
                <div class="row">
                   <div class="col-sm-12">
                        <h3>摇奖积分配置</h3>
                   </div>
                </div>
              </div>
               <div class="ibox-content">
					<form class="form-horizontal" action="${ctx}/yyy/saveYjjf"
					name="hbInitform1" id="hbInitform1" method="post">
						<div class="form-group" >
							<label class="col-sm-3 control-label">消耗积分：</label>
							<div class="col-sm-6">
							<input type="text" class="form-control" name="score_" placeholder="请输入消耗积分" maxlength="8"
									id="score_" value='${yjjf.score_ }' />
							</div>							
						</div>
						<div class="form-group">
							<label class="col-sm-5 control-label"></label>
							<div class="col-sm-6">
								<button class="btn btn-large btn-primary"  onclick="saveYjjf();return false;">&#8194;保存&#8194;</button>
							</div>
						</div>
					</form>
				</div>
          </div>
	</div>
	</div>
	</div>
	
	
	 <!-- 全局js -->
    <script src="${ctx}/js/jquery.min.js?v=2.1.4"></script>
    <script src="${ctx}/js/bootstrap.min.js?v=3.3.6"></script>
    <!-- 自定义js -->
    <script src="${ctx}/js/content.js?v=1.0.0"></script>
	<script src="${ctx}/js/plugins/layer/layer.min.js"></script>
    <script src="${ctx}/js/common.js"></script>
    <script src="${ctx}/js/plugins/toastr/toastr.min.js"></script>
	<script type="text/javascript" src="${ctx}/js/jquery.validate.min.js" ></script>
<script type="text/javascript">
$(document).ready(function() {
	toastr.options = {
			  "closeButton": false,
			  "debug": false,
			  "progressBar": false,
			  "positionClass": "toast-top-center",
			  "onclick": null,
			  "showDuration": "400",
			  "hideDuration": "1000",
			  "timeOut": "2000",
			  "extendedTimeOut": "1000",
			  "showEasing": "swing",
			  "hideEasing": "linear",
			  "showMethod": "fadeIn",
			  "hideMethod": "fadeOut"
			}
	
	$("#hbInitform").validate({
		debug : false,
		rules : {
			score : {
				required : true,
				digits:true,
				min : 1
			},
			rate : {
				required : true,
				number:true,
				min : 0,
				max : 100,
				isRate : true
			}
		},
		messages : {
			score : {
				required : "请输入中奖积分",
				digits: "中奖积分为正整数",
				min: "中奖积分不能小于1",
			},
			rate : {
				required : "请输入中奖概率",
				number : "中奖概率为大于等于0的数字",
				min: "中奖概率最小0",
				max: "中奖概率最大为100%",
				isRate:"中奖概率为大于等于0的数字"
			}
		}
	});
	
	jQuery.validator.addMethod("isRate", function(value,element) {
		var length = $("#hbInitform input[name='rate']").length;
		for(var i=0;i<length;i++){
    		var rate=$("#hbInitform input[name='rate']")[i].value;
    		if(checkIsEmpty(rate)){
				return false;
			}
			if(!checkRate(rate)){
				return false;
			}
		}
		return true; 
	}, $.validator.format("中奖概率为大于等于0的数字"));
	
	$("#hbInitform1").validate({
		rules : {
			score_ : {
				required : true,
				digits:true,
				min : 1
			}
		},
		messages : {
			score_ : {
				required : "请输入摇奖消耗积分",
				digits : "消耗积分为正整数",
				min: "消耗积分最小为1",
			}
		}
	
});
});	
    function save(){
    	
		if (!$("#hbInitform").valid()){
			return;
		}else{
	    	//表单验证
	    	 if(!checkInput()){
	   		  return; 
	   	   }
    		$.ajax({
    			type : "POST",
    			url : "saveScoreConfig", //换成你要请求的action, paperId 是url参数
    			data : $("#hbInitform").serialize(),
    			dataType:"json",
    			error : function(data) {
    				toastr.error(data.message);
    			},
    			success : function(data) {
    				if(data.state == 1){
    					toastr.success(data.message);
    				}else{
    					toastr.error(data.message);
    				}
    			}
    		});
		}
    }
   
    
  	//判断是否为浮点数
	function checkRate(str){
		var reg = /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/;
		return reg.test(str);
	}
    
    function checkInput(){
    	var length = $("#hbInitform input[name='score']").length;
    	var count = 0;
    	for(var i=0;i<length;i++){
    		var rate=$("#hbInitform input[name='rate']")[i].value;
    		var score=$("#hbInitform input[name='score']")[i].value;
			if(checkIsEmpty(rate)){
				layer.msg("中奖概率不能为空");
				return false;
			}
			if(!checkRate(rate)){
				layer.msg("中奖概率为大于等于0的数字");
				return false;
			}
			if(checkIsEmpty(score)){
				layer.msg("中奖积分不能为空");
				return false;
			}
			if(!checkNum(score)){
				layer.msg("中奖积分为正整数");
				return false;
			}
			count += rate*1;
    	}
    	if(count-100>0){
    		layer.msg("中奖概率超过100%");
			return false;
    	}
    	return true;
    }

  //新增红包概率
    function addCol(){
    	if(!$("#hbInitform").valid()){
    		return ;
    	}
    	var length = $("#hbInitform input[name='score']").length;
 	   if(length >= 10){
 		   return false;
 	   }
	   if(!checkInput()){
		  return; 
	   }		
    	var str = ' <div class="form-group">\
    					<label class="col-sm-3 control-label">中奖积分：</label>\
    					<div class="col-sm-2">\
    						<input class="form-control" type="text" name="score" id="score" placeholder="请输入中奖积分" maxlength="8"/>\
    					</div>\
    					<label class="col-sm-2 control-label">中奖概率(%)：</label>\
    					<div class="col-sm-2">\
    						<input class="form-control" type="text" id="rate" name="rate" placeholder="请输入中奖概率" maxlength="8"/>\
    					</div>\
    				<button type="button" onclick="delCol(this);return false;" class="btn btn-danger btn-xs">删除</button>\
    		</div>\ ';
    	$("#saveBtn").parent().parent().before(str);
    }

    //删除红包概率方法
    function delCol(nowTr) {
    	$(nowTr).parent().remove();
    } 
    
    function saveYjjf(){
    	if(!$("#hbInitform1").valid()){
    		return ;
    	}
    	$.ajax({
    		type : "POST",
			url : "saveYjjf", //换成你要请求的action, paperId 是url参数
			data : $("#hbInitform1").serialize(),
			dataType:"json",
			error : function(data) {
				toastr.error(data.message);
			},
			success : function(data) {
				if(data.state == 1){
					toastr.success(data.message);
				}else{
					toastr.error(data.message);
				}
			}
    		});
    	}

</script>
</body>
</html>